package com.java.huxb.day06.utils;

import com.java.huxb.day06.entity.SysUser;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @author huxb
 * @date 2020/4/4 0004 下午 2:55
 */
public class FormatUserName {
    public static void formatUserName(List list, String getIdMethod, String setNameMethod) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        if (list.size() > 0) {
            Class<?> user = list.get(0).getClass();
            Method getId = user.getMethod(getIdMethod);
            Method setName = user.getMethod(setNameMethod, String.class);
            List<SysUser> sysUserList = Utils.getSysUserList();
            if (sysUserList.size() > 0) {
                for (Object obj : list) {
                    String userId = (String) getId.invoke(obj);
                    SysUser userTemp = sysUserList.stream().filter(sysUser -> sysUser.getUserId().equals(userId)).findFirst().orElse(null);
                    if (userTemp != null) {
                        setName.invoke(obj, userTemp.getUserName());
                    }
                }
            }

        }
    }
}
